// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
	env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
	const openid = cloud.getWXContext().OPENID
	const {
		student_ID,
		password
	} = event

	const result = await db.collection('usersInfo').where({
		'profile.student_ID': student_ID,
		'profile.password': password
	}).get()
	
	if (result.data.length) {
		// 将openid加入到数据中
		result.data[0].openid = openid
		await db.collection('usersInfo').where({
			'profile.student_ID': student_ID,
			'profile.password': password
		}).update({
			data: {
				openid
			}
		})
		return [true, result.data[0]]
	} else {
		return [false]
	}
}
